#include "uart4.h"
#include <string.h>

void UART4_Config(void){
	
	GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
		
  RCC_AHB1PeriphClockCmd(UART_RX_GPIO_CLK|UART_TX_GPIO_CLK, ENABLE);

  /* ʹ�� UART ʱ�� */
  RCC_APB1PeriphClockCmd(UART_CLK, ENABLE);
  
  /* ���� PXx �� USARTx_Tx*/
  GPIO_PinAFConfig(UART_RX_GPIO_PORT,UART_RX_SOURCE, UART_RX_AF);

  /*  ���� PXx �� USARTx__Rx*/
  GPIO_PinAFConfig(UART_TX_GPIO_PORT,UART_TX_SOURCE,UART_TX_AF);

  /* ����Tx����Ϊ���ù���  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Pin = UART_TX_PIN  ;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(UART_TX_GPIO_PORT, &GPIO_InitStructure);

  /* ����Rx����Ϊ���ù��� */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = UART_RX_PIN;
  GPIO_Init(UART_RX_GPIO_PORT, &GPIO_InitStructure);
			
  /* ���ô�DEBUG_USART ģʽ */
  USART_InitStructure.USART_BaudRate = UART_BAUDRATE;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No ;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(UART, &USART_InitStructure); 
  USART_Cmd(UART, ENABLE);

  NVIC_InitTypeDef NVIC_InitStructure;
    
  // 配置UART4中断
  NVIC_InitStructure.NVIC_IRQChannel = UART_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;  // 抢占优先级
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;         // 子优先级
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
  
  // 使能UART4接收中断
  USART_ITConfig(UART, USART_IT_RXNE, ENABLE);

}

static uint8_t uart_rx_buffer[UART_RX_BUFFER_SIZE];
static volatile uint16_t uart_rx_head = 0;
static volatile uint16_t uart_rx_tail = 0;
static volatile uint16_t uart_rx_count = 0;

void UART4_IRQHandler(void)
{
    uint8_t received_data;
    
    if(USART_GetITStatus(UART, USART_IT_RXNE) != RESET)
    {
        // 清除中断标志
        USART_ClearITPendingBit(UART, USART_IT_RXNE);
        
        // 读取接收到的数据
        received_data = USART_ReceiveData(UART);
			
        // 将数据存入缓冲区
        if(uart_rx_count < UART_RX_BUFFER_SIZE)
        {
            uart_rx_buffer[uart_rx_head] = received_data;
            uart_rx_head = (uart_rx_head + 1) % UART_RX_BUFFER_SIZE;
            uart_rx_count++;
        }
				printf("%s \n",uart_rx_buffer);
    }
}


void send_data(uint8_t msg)
{
    USART_SendData(UART, (uint8_t) msg);
    while(USART_GetFlagStatus(UART, USART_FLAG_TXE) == RESET);
}

// �����ַ���
void send_string(char* str)
{
    while(*str)
    {
        USART_SendData(UART, (uint8_t) *str);
        while(USART_GetFlagStatus(UART, USART_FLAG_TXE) == RESET);
        str++;
    }
}

// ���յ����ֽ�
uint8_t get_data(void)
{
    if(USART_GetFlagStatus(UART, USART_FLAG_RXNE) != RESET)
    {
        return USART_ReceiveData(UART);
    }
    return 0;
}

// ���ն���ֽ�
uint16_t get_data_buffer(uint8_t* buffer, uint16_t max_len)
{
    uint16_t count = 0;
    
    while(count < max_len && USART_GetFlagStatus(UART, USART_FLAG_RXNE) != RESET)
    {
        buffer[count] = USART_ReceiveData(UART);
        count++;
    }
    
    return count;
}

/*********************************************END OF FILE**********************/
